package Dao.Impl;

import Dao.CarDAO;
import MyUtil.JDBCUtils;
import pojo.Car;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CarDAOImpl implements CarDAO {
    private Connection carDAoImplConn;
    private String sql;
    private PreparedStatement ps;
    private ResultSet rs;

    public CarDAOImpl() throws Exception {
        carDAoImplConn = JDBCUtils.getMyConnection();
    }

    @Override
    public List<Car> getAllCars() throws Exception {
        List<Car> carList = new ArrayList<>();

        sql = "select * from car";
        ps = carDAoImplConn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()){
            Car car = new Car();
            car.setCar_id(rs.getInt("car_id"));
            car.setModel(rs.getString("model"));
            car.setBrand(rs.getString("brand"));
            car.setLicense_plate(rs.getString("license_plate"));
            car.setStatus(rs.getString("status"));
            car.setRental_rate(rs.getInt("rental_rate"));
            car.setFuel_type(rs.getString("fuel_type"));
            car.setColor(rs.getString("color"));
            car.setMileage(rs.getInt("mileage"));
            carList.add(car);
        }
        return carList;
    }

    //查询部分
    @Override
    public List<Car> getCarByBrand(String carBrand) throws SQLException {
        List<Car> carList = new ArrayList<>();

        sql = "select * from car where brand LIKE ?";
        ps = carDAoImplConn.prepareStatement(sql);
        String fuzzyKeyword = "%"+carBrand+"%";
        ps.setString(1, fuzzyKeyword);
        rs = ps.executeQuery();
        while (rs.next()){
            Car car = new Car();
            car.setCar_id(rs.getInt("car_id"));
            car.setModel(rs.getString("model"));
            car.setBrand(rs.getString("brand"));
            car.setLicense_plate(rs.getString("license_plate"));
            car.setStatus(rs.getString("status"));
            car.setRental_rate(rs.getInt("rental_rate"));
            car.setFuel_type(rs.getString("fuel_type"));
            car.setColor(rs.getString("color"));
            car.setMileage(rs.getInt("mileage"));
            carList.add(car);
        }
        return carList;
    }

    @Override
    public boolean addCar(Car car) {
        String sql = "insert into car(model, brand, license_plate,status, rental_rate, fuel_type, color, mileage) values (?, ?, ? , ?, ?, ?,  ?, ?)";
        PreparedStatement ps = null;
        try {
            ps = carDAoImplConn.prepareStatement(sql);
            ps.setString(1, car.getModel()); // Assuming this is the correct model field.
            ps.setString(2, car.getBrand()); // If brand should be included in the SQL statement.
            ps.setString(3, car.getLicense_plate()); // If license plate should be included in the SQL statement.
            ps.setString(4, "可租"); // If status should be included in the SQL statement.
            ps.setInt(5, car.getRental_rate()); // Assuming this is the correct field for rental rate.
            ps.setString(6, car.getFuel_type()); // If fuel type should be included in the SQL statement.
            ps.setString(7, car.getColor()); // If color should be included in the SQL statement.
            ps.setInt(8, car.getMileage()); // Assuming this is the correct field for mileage.
            int count = ps.executeUpdate();
            if (count != 0) {
                return true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace(); // You may want to use a logging framework instead for production code.
        } finally {
            try { if (ps != null) ps.close(); } catch (SQLException ex) { /* ignored */ } // Close prepared statement in finally block for resource management.
        }
        return false;
    }

    @Override
    public void updateCar(Car car) {

    }

    @Override
    public void deleteCar(int carId) throws SQLException {

        sql = "DELETE FROM car WHERE car_id = ?";
        ps = carDAoImplConn.prepareStatement(sql);
        ps.setString(1, String.valueOf(carId));
        ps.executeUpdate();
    }

}
